package com.eps.module.testdagger2_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.eps.module.testdagger2_2.component.LoginComponent;

import javax.inject.Inject;

public class LoginActivity extends AppCompatActivity {


    // Reference to the Login graph
    LoginComponent loginComponent;

    @Inject
    LoginViewModel loginViewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        ((AppApplication) getApplication()).getApplicationComponent().injectX(this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Creation of the login graph using the application graph
        loginComponent = ((AppApplication) getApplicationContext())
                .getApplicationComponent().loginComponent().create();

        // Make Dagger instantiate @Inject fields in LoginActivity
        loginComponent.inject(this);
//        testSingleInstance();



    }

    private void testSingleInstance() {
        Log.d("chenTest", "localValue: " +  loginViewModel.userRepository.getUserLocalDataSource().getValue());
        Log.d("chenTest", "serviceValue: "
                +  loginViewModel.userRepository.getUserRemoteDataSource().getLoginRetrofitService().getValue());

        Log.d("chenTest", "login Value: " + loginViewModel.getValue());
        startActivity(new Intent(this, SecondActivity.class));
        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("chenTest", "get localValue: " +  loginViewModel.userRepository.getUserLocalDataSource().getValue());
                Log.d("chenTest", "get serviceValue: "
                        +  loginViewModel.userRepository.getUserRemoteDataSource().getLoginRetrofitService().getValue());
                Log.d("chenTest", "get login Value: " + loginViewModel.getValue());

            }
        });
    }
}